Binary Tree

二叉树的定义、实现、遍历、输出、应用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <iostream>
#include <stack>
#include <queue>
using namespace std;

struct BiTNode {
int data;
BiTNode *lchild;
BiTNode *rchild;
};

class BinaryTree {
private:
BiTNode *bt;
int RCreate(BiTNode *p, int k, int end);
public:
void CreateBiTree(int end);
BinaryTree() { bt = NULL; }
void Release(BiTNode *root);
~BinaryTree();
int PreTraverse(BiTNode *p);
int InTraverse(BiTNode *p);
int PostTraverse(BiTNode *p);
void PreOrderTraverse();
void InOrderTraverse();
void PostOrderTraverse();
void LevelOrderTraverse();
void LeafCount();
void BiTreeDepth();
BiTNode *GetTreeNode(int item, BiTNode *lptr, BiTNode *rptr);
BiTNode *CopyTree(BiTNode *p);
int CompareTree(BiTNode *t1, BiTNode *t2);
BiTNode *GetRoot();
void BiTreeDisplay(BiTNode *bt, int level = 1);
};
//创建二叉树递归函数
int BinaryTree::RCreate(BiTNode * p, int k, int end) {
BiTNode *q;
int e;
cin >> e;
if (e != end) {
q = new BiTNode;
q->data = e;
q->lchild = NULL;
q->rchild = NULL;
if (k == 1) p->lchild = q;
if (k == 2) p->rchild = q;
RCreate(q, 1, end);
RCreate(q, 2, end);
}
return 0;
}
//按先序序列创建二叉树
void BinaryTree::CreateBiTree(int end) {
cout << "请按先序序列的顺序输入二叉树,0为空指针域标志:" << endl;
BiTNode *p;
int e;
cin >> e;
if (e == end) return;
p = new BiTNode;
if (!p) {
cout << "申请内存失败!" << endl;
exit(-1);
}
p->data = e;
p->lchild = NULL;
p->rchild = NULL;
bt = p;
RCreate(p, 1, end);
RCreate(p, 2, end);
}
//先序遍历递归函数
int BinaryTree::PreTraverse(BiTNode *p) {
if (p != NULL) {
cout << p->data << ' ';
PreTraverse(p->lchild);
PreTraverse(p->rchild);
}
return 0;
}
//中序遍历递归函数
int BinaryTree::InTraverse(BiTNode *p) {
if (p != NULL) {
InTraverse(p->lchild);
cout << p->data << ' ';
InTraverse(p->rchild);
}
return 0;
}
//后续遍历递归函数
int BinaryTree::PostTraverse(BiTNode * p) {
if (p != NULL) {
PostTraverse(p->lchild);
PostTraverse(p->rchild);
cout << p->data << ' ';
}
return 0;
}
//销毁二叉树递归函数
void BinaryTree::Release(BiTNode *root) {
if (root != NULL) {
Release(root->lchild);
Release(root->rchild);
delete root;
}
}
//析构函数
BinaryTree::~BinaryTree() {
Release(bt);
}
//非递归先序遍历二叉树
void BinaryTree::PreOrderTraverse() {
cout << "先序(非递归)遍历二叉树:";
BiTNode *p = bt;
stack<BiTNode> s;
while (p || !s.empty()) {
if (p) {
cout << p->data << ' ';
s.push(*p);
p = p->lchild;
}
else {
p = new BiTNode;
*p = s.top();
s.pop();
p = p->rchild;
}
}
cout << endl;
}
//非递归中序遍历二叉树
void BinaryTree::InOrderTraverse() {
cout << "中序(非递归)遍历二叉树:";
BiTNode *p = bt;
stack<BiTNode> s;
while (p || !s.empty()) {
if (p) {
s.push(*p);
p = p->lchild;
}
else {
p = new BiTNode;
*p = s.top();
s.pop();
cout << p->data << ' ';
p = p->rchild;
}
}
cout << endl;
}
//非递归后序遍历结点类型
struct BiTNode1 {
BiTNode *bt;
int tag;
};
//非递归后序遍历二叉树
void BinaryTree::PostOrderTraverse() {
cout << "后序(非递归)遍历二叉树:";
BiTNode *p = bt;
stack<BiTNode1> s;
BiTNode1 *temp;
while (p || !s.empty()) {
if (p) {
BiTNode1 *t = new BiTNode1;
t->bt = p;
t->tag = 1;
s.push(*t);
p = p->lchild;
}
else {
temp = new BiTNode1;
*temp = s.top();
s.pop();
if (temp->tag == 1) {
temp->tag = 2;
s.push(*temp);
p = new BiTNode;
p = temp->bt->rchild;
}
else {
cout << temp->bt->data << ' ';
p = NULL;
}
}
}
cout << endl;
}
//层次遍历二叉树
void BinaryTree::LevelOrderTraverse() {
cout << "层次遍历二叉树:";
queue<BiTNode> q;
BiTNode *t;
if (bt) {
q.push(*bt);
while (!q.empty()) {
t = new BiTNode;
*t = q.front();
q.pop();
if (t) cout << t->data << ' ';
if (t->lchild) q.push(*t->lchild);
if (t->rchild) q.push(*t->rchild);
}
}
cout << endl;
}
//计算叶子的个数递归函数
void Leaf(BiTNode *p, int &count) {
if (p) {
Leaf(p->lchild, count);
Leaf(p->rchild, count);
if (p->lchild == NULL && p->rchild == NULL) count++;
}
}
//后序遍历计算叶子的个数
void BinaryTree::LeafCount() {
int count = 0;
Leaf(bt, count);
cout << "叶子的个数为" << count << endl;
}
//计算深度递归函数
void Depth(BiTNode *p, int level, int &depth) {
if (p->lchild) Depth(p->lchild, level + 1, depth);
if (p->rchild) Depth(p->rchild, level + 1, depth);
if (level > depth) depth = level;
}
//后序遍历计算深度
void BinaryTree::BiTreeDepth() {
int depth = 0;
if (bt) {
Depth(bt, 1, depth);
}
cout << "二叉树的深度为" << depth << endl;
}
//获取二叉树的一个结点
BiTNode *BinaryTree::GetTreeNode(int item, BiTNode * lptr, BiTNode * rptr) {
BiTNode *p = new BiTNode;
p->data = item;
p->lchild = lptr;
p->rchild = rptr;
return p;
}
//先序遍历复制一棵二叉树
BiTNode *BinaryTree::CopyTree(BiTNode *p) {
BiTNode *newlptr, *newrptr;
if (p == NULL) return NULL;
if (p->lchild) newlptr = CopyTree(p->lchild);
else newlptr = NULL;
if (p->rchild) newrptr = CopyTree(p->rchild);
else newrptr = NULL;
bt = GetTreeNode(p->data, newlptr, newrptr);
}
//先序遍历判断两棵二叉树是否相等
int BinaryTree::CompareTree(BiTNode * t1, BiTNode * t2) {
if (t1 == NULL && t2 == NULL) return 1;
else if (t1->data == t2->data&&CompareTree(t1->lchild, t2->lchild) && CompareTree(t1->rchild, t2->rchild)) return 1;
else return 0;
}
//获取根结点
BiTNode* BinaryTree::GetRoot() {
if (bt != NULL) return bt;
return NULL;
}
//反中序递归横向树状显示一棵二叉树
void BinaryTree::BiTreeDisplay(BiTNode * bt, int level) {
if (bt) {
BiTreeDisplay(bt->rchild, level + 1);
for (int i = 0; i < level; i++) cout << " ";
cout << bt->data << endl;
BiTreeDisplay(bt->lchild, level + 1);
}
}

int main() {
BinaryTree bit;
BinaryTree bit1;
while (1) {
bit.CreateBiTree(0);
bit.BiTreeDisplay(bit.GetRoot(),0);

cout << "先序(递归)遍历二叉树:";
bit.PreTraverse(bit.GetRoot());
cout << endl;

cout << "中序(递归)遍历二叉树:";
bit.InTraverse(bit.GetRoot());
cout << endl;

cout << "后序(递归)遍历二叉树:";
bit.PostTraverse(bit.GetRoot());
cout << endl;

bit.PreOrderTraverse();
bit.InOrderTraverse();
bit.PostOrderTraverse();
bit.LevelOrderTraverse();

bit.LeafCount();
bit.BiTreeDepth();

bit1.CopyTree(bit.GetRoot());
bit1.BiTreeDisplay(bit1.GetRoot(), 0);
if (bit1.CompareTree(bit.GetRoot(), bit1.GetRoot())) cout << "相等,拷贝成功" << endl;
cout << endl;
}
return 0;
}